﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Common
{
    /// <summary>  
    /// 委托  
    /// </summary>  
    /// <param name="e"></param>  
    /// <returns></returns>  
    public delegate int EventPagingHandler(EventPagingArg e);
    public partial class pageControl : UserControl
    {
        public pageControl()
        {
            InitializeComponent();
        }
        public event EventPagingHandler EventPaging;
        private object _sourceobj;//数据源  

        public object Sourceobj
        {
            get { return _sourceobj; }
            set { _sourceobj = value; }
        }
        //每页记录数  
        private int _pageSize = 20;

        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                _pageSize = value;
                GetPageCount();
            }
        }
        //总记录数  
        private int _PageTotalNum = 0;

        public int PageTotalNum
        {
            get { return _PageTotalNum; }
            set
            {
                _PageTotalNum = value;
                GetPageCount();
            }
        }
        //页数=总数/每页记录数  
        private int _pageCount = 0;

        public int PageCount
        {
            get { return _pageCount; }
            set { _pageCount = value; }
        }
        //当前页  
        private int _pageCurrent = 0;

        public int PageCurrent
        {
            get { return _pageCurrent; }
            set { _pageCurrent = value; }
        }
        //页数  
        private void GetPageCount()
        {
            if (this.PageTotalNum > 0)
            {
                this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.PageTotalNum) / Convert.ToDouble(this.PageSize)));
            }
            else
            {
                this.PageCount = 0;
            }
        }
        //绑定源  
        public void Bind()
        {
            if (this.Sourceobj != null)
            {
                //绑定数据源  
                this.bindingSource1.DataSource = this.Sourceobj;
                this.bindingNavigator1.BindingSource = this.bindingSource1;
            }
            this.FirstItem.Enabled = true;
            this.PreviousItem.Enabled = true;
            this.NextItem.Enabled = true;
            this.LastItem.Enabled = true;

            if (this.EventPaging != null)
            {
                //通过委托事件方法，触发引用窗体绑定方法获取  
                this.PageTotalNum = this.EventPaging(new EventPagingArg());
            }
            if (this.PageCurrent > this.PageTotalNum)
            {
                this.PageCurrent = this.PageTotalNum;
            }
            if (this.PageCount == 1)
            {
                this.PageCurrent = 1;
            }
            if (PageCurrent == 1)
            {
                this.FirstItem.Enabled = false;
                this.PreviousItem.Enabled = false;
            }
            if (PageCurrent == PageCount)
            {
                this.NextItem.Enabled = false;
                this.LastItem.Enabled = false;
            }
            if (this.PageTotalNum == 0)
            {
                this.FirstItem.Enabled = false;
                this.PreviousItem.Enabled = false;
                this.NextItem.Enabled = false;
                this.LastItem.Enabled = false;
            }

            this.TextPositionItem.Text = PageCurrent.ToString();
            this.TextPositionItem.Enabled = true;
            this.labelCountItem.Text = "共 " + PageCount + " 页";
            this.labelCountItem.Enabled = false;
        }
        //首页  
        private void FirstItem_Click(object sender, EventArgs e)
        {
            PageCurrent = 1;
            this.Bind();
        }
        //上一页  
        private void PreviousItem_Click(object sender, EventArgs e)
        {
            PageCurrent--;
            if (PageCurrent <= 0)
            {
                PageCurrent = 1;
            }
            this.Bind();
        }
        //下一页  
        private void NextItem_Click(object sender, EventArgs e)
        {
            this.PageCurrent++;
            if (PageCurrent > PageCount)
            {
                PageCurrent = PageCount;
            }
            this.Bind();
        }
        //尾页  
        private void LastItem_Click(object sender, EventArgs e)
        {
            PageCurrent = PageCount;
            this.Bind();
        }
    }
    /// <summary>  
    /// 自定义事件数据基类(当前页数？)  
    /// </summary>  
    public class EventPagingArg : EventArgs
    {
        public EventPagingArg()
        {
        }
    } 
}
